<template>
  <div class="inside-page">
    <div class="user content">
      <p class="detail-title">企业信息</p>
      <div class="com" style="min-height: 400px">
        <!-- <Com :info="info" :inc="inc" :sizes="sizes" :atts="atts" @submit_com="submit_com" /> -->
        <Com :info="info" :inc="inc" :sizes="sizes" :atts="atts" />
      </div>
      <div class="com" style="min-height: 240px">
        <Intro :description="info.description" @submit_desc="submit_desc" />
      </div>
      <div class="com" style="min-height: 120px">
        <Welfare :welfare-list="info.welfare" :welfares="welfares" />
      </div>
      <div class="com" style="min-height: 120px">
        <License
          :business="info.business_license"
          @submit_license="submit_license"
        />
      </div>
    </div>
    <div class="submit">
      <button class="confirm" type="button" @click="submit">申请发布</button>
      <button class="cancel" type="button" @click="cancel">取消</button>
    </div>
  </div>
</template>

<script>
import { getMyCompany, updateCompany } from '@/api/users';
import { provinceAndCityData, CodeToText, TextToCode} from 'element-china-area-data'
export default {
  asyncData: ({ req }) => ({
  }),
  components: {},
  data () {
    return {
      info: {
        logo: '',
        cname: '',
        size_data: '',
        sizeid: '',
        industry_data: '',
        industryid: '',
        attr_data: '',
        attrid: '',
        phone: '',
        email: '',
        business_number: '',
        business_license: '',
        address: '',
        city: '',
        province: '',
        city_name: '',
        province_name: '',
        locat: '',
        description: '',
        welfare: []
      },
      welfares: [],
      sizes: [],
      inc: [],
      atts: [],
      logo: '',
    }
  },
  head () {
    return {
      titleTemplate: (titleChunk) => {
        return `${titleChunk} - 大学生找工作、找实习`;
      }
    }
  },
  created () {
    this.fetchData();
  },
  mounted () {
  },
  watch: {
  },
  computed: {
  },
  methods: {
    fetchData () {
      getMyCompany({}).then(res => {
        if (res.code === 200) {
          if (res.data) {
            const data = res.data;
            data.data && data.data.welfare === '' && (data.data.welfare = [])
            data.data && (this.info = data.data);
            this.welfares = data.welfares;
            this.inc = data.inc;
            this.sizes = data.sizes;
            this.atts = data.atts;
            if (this.info.province) {
              this.info.province_name = CodeToText[this.info.province];
            }
            if (this.info.city) {
              this.info.city_name = CodeToText[this.info.city];
            }
            this.info.locat = [this.info.province.toString(), this.info.city.toString()];
            console.log(this.info);
          }
        } else if (res.code === 400) {
          this.$message.info(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err || '网络错误！')
      })
    },
    // submit_com (logo) { // 头像
    // this.logo = logo
    // },
    submit_desc (data) { // 简介
      this.info.description = data;
    },
    submit_license (data) { // 营业执照
      this.info.business_license = data;
    },
    cancel () { // 取消
      this.$router.go(-1);
    },
    submit () { // 提交
      const {
        logo,
        cname,
        sizeid,
        attrid,
        industryid,
        phone,
        email,
        description,
        city,
        province,
        address,
        business_number,
        business_license,
        welfare,
      } = this.info;

      const formData = new FormData();
      formData.append('cname', cname);
      formData.append('sizeid', sizeid);
      formData.append('attrid', attrid);
      formData.append('industryid', industryid);
      formData.append('description', description);
      formData.append('phone', phone);
      formData.append('email', email);
      formData.append('city', city);
      formData.append('province', province);
      formData.append('address', address);
      formData.append('logo', logo);
      formData.append('business_license', business_license);
      formData.append('business_number', business_number);
      // this.logo && formData.append('logo', this.logo);
      // formData.append('business_number', business_number);
      welfare && formData.append('welfare', welfare.map(s => s.id).join(','));
      // this.business_license && formData.append('business_license', this.business_license);
      try {
        updateCompany(formData, true).then(res => {
          if (res.code === 200) {
            this.showView = true;
            this.$message.success(res.msg || '保存成功！');
          } else {
            this.$message.error(res.msg || '网络错误, 稍后再试！');
          }
        }).catch(err => {
          console.log(err)
          this.$message.error(err || '网络错误, 稍后再试！');
        })
      } catch (err) {
        console.dir(err)
        this.errors = err.response.data.errors
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.user {
  padding-top: 40px;
  position: relative;
}
</style>
